<?php  
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ 
include_once("includes/db.inc");
include_once("includes/functions.inc");
include ("includes/header.inc");

$action=$_GET['action'];

if ($_POST['code']) $code=$_POST['code']; 
elseif ($_GET['code']) $code=$_GET['code']; 
if ($_POST['code_out']) $code_out=return_isbn($_POST['code_out']); 
elseif ($_GET['code_out']) $code_out=return_isbn($_GET['code_out']); 
if ($_POST['etapeA']) $etapeA=$_POST['etapeA']; 
elseif ($_GET['etapeA']) $etapeA=$_GET['etapeA']; 
if ($_GET['deja']) $deja = $_GET['deja'];
if ($_GET['pret_id']) $pret_id = $_GET['pret_id'];
if ($_GET['etat_livre_in']) $etat_livre_in = $_GET['etat_livre_in'];

if (!($action))
{
   echo "<center><table><tr>";
   echo "<td><A HREF=\"pret.php?action=sortie\"><img src=images/out.png border=0></a></td>";
   echo "<td><CENTER><H2><A HREF=\"pret.php?action=sortie\">";
   echo _("SORTIE");
   echo "</A></H2><BR>";
   echo "<H2><A HREF=\"pret2.php?action=entree\">";
   echo _("ENTREE");
   echo "</A></H2><BR></center>";
   echo "</td>";
   echo "<td><A HREF=\"pret2.php?action=entree\"><img src=images/in.png border=0></a></td></tr></table></center>";
   ?>
   <br><br>
   <div>
   <span id=left-text>
   <A HREF="index.php">
   <?php echo _("retour menu principal"); ?>
   </A></span>
   </div>
   <br>
   <?php
   include("includes/footer.inc");
   exit;
}

if(!($code))
{
   ?>
   <CENTER>
   <FORM action=pret.php?action=<?php echo $action; ?> METHOD=POST>
   <h3>
   <?php echo _("CODE DU LECTEUR"); ?>
   : <INPUT TYPE="text" NAME="code" SIZE=15></h3>
   </CENTER>
   <?php
}
else 
{
        $req="select id, nom, prenom from eleves where code='$code'";
        if(!($resultat2 = biblio_db_query($req)))
	{ print "PROBLEME : " .biblio_db_error(); }
	
	while ($myrow = biblio_db_fetch_array($resultat2)) 
	{ 
	        $eleve_ID=$myrow['id'];
	        $eleve_nom=$myrow['nom'];
	        $eleve_prenom=$myrow['prenom'];
	}        
	echo "<CENTER><H3>$eleve_prenom $eleve_nom</H3></CENTER>";
        
        $requete = "SELECT t1.date_out, t2.nom, t3.etat FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_ID inner join etats as t3 on t3.id = t1.etat_out_ID where eleve_ID=$eleve_ID and date_int is NULL order by date_out"; 

	if(!($resultat = biblio_db_query($requete)))
	{ 
	  #print "PROBLEME : " .biblio_db_error(); 
	}
	else 
	{  if (mysql_num_rows($resultat)>0)
	   {    
	        $i=0;
	        echo "<CENTER><TABLE><tr><th>";
		echo _("Date de sortie");
		echo "</th><th>";
		echo _("Titre");
		echo "</th><th>";
		echo _("Etat");
		echo "</th>";
	        while ($myrow = biblio_db_fetch_array($resultat)) 
	        {
		       $tab_mois = array('',_('janvier'),_('f&eacute;vrier'),_('mars'),_('avril'),_('mai'),_('juin'),_('juillet'),
		       	_('ao&ucirc;t'),_('septembre'),_('octobre'),_('novembre'),_('decembre'));
	               $date_sort = $myrow['date_out'];
	               ereg("(....)-(..)-(..)$",$date_sort,$match);
	               $jour  = $match[3];
	               $mois  = sprintf("%d",$match[2]);
	               $annee = $match[1];
	               $date_sort =  "$jour " . $tab_mois[$mois] . " $annee";
	               $i++;
	               if ($i%2 == 1) {$col="#DFE3EF";}
	               else {$col="lightyellow";}
                       echo "<TR bgcolor=$col><TD>".$date_sort."</TD><TD>".$myrow['nom']."</TD><TD>".$myrow['etat']."</TD>";
                }
           }
           else echo "<CENTER><B>" . _("PAS ENCORE DE LIVRE EN PRET") . "</B></CENTER>";
        }
        
        echo "</TABLE></CENTER><HR>";
        $requete = "SELECT * FROM eleves where code='$code' "; 
	if(!($resultat = biblio_db_query($requete)))
	{ print "PROBLEME : " .biblio_db_error(); }
	else 
	{  if (mysql_num_rows($resultat)>0)
	   {
                
                if($etapeA != 1)
                {
                  ?>
                  <CENTER>
                  <FORM action=pret.php?action=<?php echo $action; ?> METHOD=POST>
                  <h4>
		  <?php echo _("CODE DU LIVRE SORTANT"); ?>: <INPUT TYPE="text" NAME="code_out" SIZE=15></h4>
                  <INPUT TYPE="hidden" NAME="code" VALUE="<?php echo $code; ?>">
                  <INPUT TYPE="hidden" NAME="etapeA" VALUE="1">
                  </CENTER>
                  <?php
                }
                else
                {
                                    
                  $date_out = date("Y-m-d");
	          $req="select id from livres where ISBN='$code_out'";
                  if(!($resultat2 = biblio_db_query($req)))
	          { print _("PROBLEME")." $req: " .biblio_db_error(); }
		  elseif (biblio_db_num_rows($resultat2) == 0)
		  {
	             echo "<CENTER><FONT COLOR=RED><BLINK><B>";
		     echo _("CE LIVRE N'EST PAS CONNU DANS CETTE BIBLIOTHEQUE !!");
		     echo "</B></blink></FONT></CENTER>"; 
		  }
		  else
		  {
	          #$req="select t1.id livreid, t1.nom, t1.auteur, t1.edition, t2.id etatid, t2.etat from livres as t1 inner join etats as t2 on t2.id = t1.etat_ID inner join prets as t3 on t3.livre_ID = t1.id where t1.ISBN='$code_out' and t3.date_out is NULL";                  
	          $req="select t1.id livreid, t1.nom, t1.auteur, t1.edition, t2.id etatid, t2.etat from livres as t1 inner join etats as t2 on t2.id = t1.etat_ID where t1.ISBN='$code_out'";                  
	          #echo "<HR>$req<HR>";
                  if(!($resultat2 = biblio_db_query($req)))
	          { print "PROBLEME $req: " .biblio_db_error(); }
	          while ($myrow = biblio_db_fetch_array($resultat2)) 
	          { 
	            $str= "<CENTER><B>" . $myrow['nom'] ."</B>, " . $myrow['auteur'] .", " . $myrow['edition'] .", en <U>" . $myrow['etat'] ."</U> &eacute;tat est sorti !</CENTER>";
	            $livreid = $myrow['livreid'];
	            $etatid=$myrow['etatid'];
	          }
	          $req="select id from prets where livre_ID=$livreid and date_int is NULL";
	          $req2="select id from livres where id=$livreid and noout = 0";                  
	          #echo "<HR>$req<HR>";
                
                  if(!($resultat2 = biblio_db_query($req)))
	          { print "PROBLEME $req: " .biblio_db_error(); }
	          if (mysql_num_rows($resultat2)!=0) 
	          { 
                    if($deja == "on")
                    {
			$date_in = date("Y-m-d");
                        if (!($etat_livre_in)) { $etat_livre_in = $etatid; }
   	                $requete2 = "UPDATE prets set date_int = '$date_in', etat_in_ID = $etat_livre_in where id = $pret_id";
       		        if(!($resultat2 = biblio_db_query($requete2)))
	        	{ print "PROBLEME $requete2: " .biblio_db_error(); }	
	        	put_log(3,"PRET IN: $requete",$user);
                        $req2="insert into prets ( eleve_ID,livre_ID,date_out,etat_out_id) values ('$eleve_ID','$livreid','$date_out','$etatid')";

			if(!($resultat2 = biblio_db_query($req2)))
                        { print "PROBLEME : " .biblio_db_error(); }
                        put_log(2,"PRET OUT: $requete",$user);
                        echo "$str<BR><BR><A HREF=\"pret.php?action=sortie&code=$code\">emprunt d'un autre livre</A>";
                    }
                    else
                    {       
                    	$requete2 = "SELECT t2.code, t2.nom, t2.prenom,t1.livre_ID, t1.id, t1.etat_in_ID, t1.etat_out_ID, t4.classe FROM prets as t1 inner join eleves as t2 on t2.id = t1.eleve_ID inner join livres as t3 on t3.id=t1.livre_ID inner join classes as t4 on t4.id = t2.classe_id where t3.ISBN='$code_out' and date_int is NULL"; 
	               	    if(!($resultat2 = biblio_db_query($requete2)))
			    { print "PROBLEME : " .biblio_db_error(); }
			    else 
			    {  if (mysql_num_rows($resultat2)>0)
		               {    
	            	         	while ($myrow = biblio_db_fetch_array($resultat2)) 
		     			{
			                   $code_el        =$myrow['code'];
			                   $nom_el         =$myrow['nom'];
	  		      	           $prenom_el      =$myrow['prenom'];
	    		   	           $etat_livre_in  =$myrow['etat_in_ID'];
	      			           $etat_livre_out =$myrow['etat_out_ID'];
	                   		   $livre_ID_q     =$myrow['livre_ID'];
					   $classe_el	   =$myrow['classe'];
					   $pret_id	     =$myrow['id'];
	             			}
		             		echo "<CENTER><FONT COLOR=RED><BLINK><B>";
					echo _("CE LIVRE EST DEJA EMPRUNTE PAR ");
					echo "<U>$prenom_el $nom_el ";
					echo _("de");
					echo "$classe_el</U>!<BR>";
					echo _("CLIQUEZ");
					echo "</BLINK> ";
			     		echo "<A HREF=\"pret.php?action=sortie&etapeA=1&deja=on&code=$code&code_out=$code_out&pret_id=$pret_id&etat_livre_in=$etat_livre_out\">";
					echo _("ICI");
					echo "</A> <BLINK>";
					echo _("POUR LE RENTRER");
					echo "</blink></b></FONT></CENTER>"; 
	                      }
 	                   }
		     }
  	           }
	          else
	          { if(!($resultat2 = biblio_db_query($req2)))
	            {  print "PROBLEME $req2: " .biblio_db_error(); }
	            if (mysql_num_rows($resultat2)==0) 
	            { echo "<CENTER><FONT COLOR=RED><BLINK><B>";
		      echo _("CE LIVRE NE PEUT SORTIR DE LA BIBLIOTHEQUE !!");
		      echo "</blink></b></FONT></CENTER>"; }	                     
	            else 
	            {  	            
	                        $req2="insert into prets ( eleve_ID,livre_ID,date_out,etat_out_id) values ('$eleve_ID','$livreid','$date_out','$etatid')";
	                        if(!($resultat2 = biblio_db_query($req2)))
	                        { print "PROBLEME : " .biblio_db_error(); }
	                        put_log(2,"PRET OUT : $requete",$user);
	                        echo "$str<BR><BR><A HREF=\"pret.php?action=sortie&code=$code\">";
				echo _("emprunt d'un autre livre");
				echo "</A>";
                    }
                  }
		  }
                }
           }
           else { echo "<H3><CENTER>";
	   	  echo _("Ce code ne correspond &agrave; aucun lecteur !");
		  echo "</CENTER></H3>"; }
          }
}

?>
<br><br>
<div>
<span id=left-text>
<A HREF="pret.php">
<?php echo _("retour menu pr&ecirc;ts"); ?>
</A><br>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
<br>
<?php
include("includes/footer.inc");
?>
